package com.yueya.business.senior.model;

import java.util.Map;

import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.yueya.common.util.DateUtil;
import com.yueya.common.util.UUIDGenerator;

import net.sf.json.JSONObject;

/**
 * 老人基本信息
 * @author todd
 *
 */
@SuppressWarnings("serial")
public class SeniorBase extends Model<SeniorBase> {
	
	public static  SeniorBase dao = new SeniorBase();
	
	public boolean save(SeniorBase dao) {
		dao.set("create_time",DateUtil.getDate(DateUtil.timeFormat));
		return dao.save();
	}

	 
    public boolean update(SeniorBase dao) {
    	dao.set("update_time",DateUtil.getDate(DateUtil.timeFormat));
		return dao.update();
	}
    
    public SeniorBase findByBedid(String bedid) {
    	return dao.findFirst("select s.*  from jg_senior_base s INNER JOIN jg_stay rsb on s.senior_id = rsb.senior_id INNER JOIN jg_bed b on b.bed_id = rsb.bed_id   where b.bed_id =?",bedid);
    }
    
    public SeniorBase findByMap(Map<String,Object> queryMap) {
    	String sql ="select sb.* from jg_senior_base sb  where 1=1" ;
    	if(queryMap!=null) {
    		if(queryMap.get("seniorid")!=null) {
    			sql+=" and senior_id='"+queryMap.get("seniorid").toString()+"'";
    		}
    		if(queryMap.get("idcard")!=null) {
    			sql+=" and id_card='"+queryMap.get("idcard").toString()+"'";
    		}
    		
    		
    	}
    	return dao.findFirst(sql);
    }
    
    
    
    public SeniorBase getDetailById(String id) {
    	
    	String sql = "select ns.nursing_standards,bed.bed_no,(select floor_name from jg_floor  where bed.floor_id = floor_id) floor_name, ";
		sql+="(select h.house_name  from jg_house h  where bed.house_id = h.house_id) house_name, ";
		sql+="(select r.room_name  from jg_room r  where bed.room_id = r.room_id) room_name,  ";
		sql+="sb.*   from  jg_senior_base sb  ";
		sql+=" LEFT JOIN jg_nursing_standards ns on sb.nursing_standards_id = ns.nursing_standards_id " ;
		sql+=" LEFT JOIN jg_bed bed on sb.bunk_id = bed.bed_id  " ;
		sql+=" where sb.del_flag='0' and  sb.senior_id =?  " ;
	
		SeniorBase r = dao.findFirst(sql,id);
    	
    	return r;
    }
    
    @SuppressWarnings("rawtypes")
	public Page paginate (int pageNumber,int pageSize,JSONObject jo) {
      
    	String select = "select ns.nursing_standards,bed.bed_no,(select floor_name from jg_floor  where bed.floor_id = floor_id) floor_name, ";
		select+="(select h.house_name  from jg_house h  where bed.house_id = h.house_id) house_name, ";
		select+="(select r.room_name  from jg_room r  where bed.room_id = r.room_id) room_name,  ";
		select+="sb.*  ";
		StringBuffer sql = new StringBuffer(" from jg_senior_base sb   ");
		sql.append("LEFT JOIN jg_nursing_standards ns on sb.nursing_standards_id = ns.nursing_standards_id ");
		sql.append("LEFT JOIN jg_bed bed on sb.bunk_id = bed.bed_id  where sb.del_flag='0' ");
		// 名字
		if (jo.containsKey("name")) {
			String name = jo.getString("name");
			if (StrKit.notBlank(name)) {
				sql.append(" and (sb.name like '%" + name + "%' or sb.id_card like  '%"+name+"%'  )");
			}
		}
		// 房号
		if (jo.containsKey("roomid")) {
			String roomid = jo.getString("roomid");
			if (StrKit.notBlank(roomid)) {
				sql.append(" and bed.room_id = '"+roomid+"' ");
			}
		}else if (jo.containsKey("floorid")) {
			
			String floorid = jo.getString("floorid");
			if (StrKit.notBlank(floorid)) {
				sql.append(" and bed.floor_id  = '"+floorid+"' ");
			}
		}else if (jo.containsKey("houseid")) {
			String houseid = jo.getString("houseid");
			if (StrKit.notBlank(houseid)) {
				sql.append(" and bed.house_id  ='"+houseid+"'");
			}
		}
		sql.append(" order by sb.create_time desc");
		return dao.paginate(pageNumber, pageSize, select, sql.toString());
    }
	
    
    
}
